<template>
  <div class="script-card" @mouseover="isHovered = true" @mouseleave="isHovered = false">
    <!-- 删除按钮 -->
    <button v-if="isHovered" class="delete-button" @click="showDeleteModal=true">&times;</button>
    <img :src="image" alt="Script Image" class="script-card-image">
    <div class="script-card-content">
      <div class="script-author">

        <span class="author-name">{{ username }}</span>
        <span class="post-time">{{ play_time }}</span>
      </div>
      <h3 class="script-title">{{ title }}</h3>
      <p class="script-content">{{ content }}</p>
      <div class="script-card-footer">
        <div class="interaction-icons">
        </div>
        <button class="play-button" @click="continueScript">CONTINUE</button>
      </div>
    </div>
  </div>
  <!-- 删除确认模态框 -->
  <div v-if="showDeleteModal" class="delete-modal-overlay">
    <div class="modal-content">
      <span class="close" @click="showDeleteModal = false">&times;</span>
      <h3>确认删除</h3>
      <p>你确定要删除这个存档吗？此操作不可撤销。</p>
      <button @click="deleteConversation" class="confirm-button">确认删除</button>
      <button @click="showDeleteModal = false" class="cancel-button">取消</button>
    </div>
  </div>
</template>

<script>
import {deleteConversationService} from "@/api/AIChat";

export default {
  name: 'Conversation',
  props: {
    image: String,
    username: String,
    play_time: String,
    title: String,
    content: String,
    tags: String,
    script_id: Number,
    conversation_id: Number
  },
  data() {
    return {
      isHovered: false,// 控制是否显示删除按钮
      showDeleteModal: false,
    }
  },
  methods: {
    continueScript() {
      // todo 加载存档

      // 页面跳转，传递 scriptId 和 defaultConversationId
      this.$router.push({
        name: 'AIOChat',
        params: {
          scriptId: this.script_id, // 替换为实际的 scriptId
          conversationId: this.conversation_id // 替换为实际的 conversationId
        }
      })
    },
    async deleteConversation() {
      try {
        await deleteConversationService(this.conversation_id);
        await this.$emit('refresh'); // 通知父组件刷新数据
        this.showDeleteModal = false;
      } catch (error) {
        console.error("Error deleting conversation:", error);
      }
    }
  }
}
</script>

<style scoped>
.script-card-image {
  width: 100%;
  height: 200px;
  object-fit: cover;
}

.script-card-content {
  padding: 15px;
  color: #fff;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  height: 100%;
}

.script-author {
  display: flex;
  align-items: center;
  margin-bottom: 10px;
}

.author-name {
  font-weight: bold;
  margin-right: 10px;
}

.post-time {
  color: #888;
  font-size: 12px;
}

.script-title {
  font-size: 1.2em;
  margin-bottom: 10px;
}

.script-content {
  font-size: 0.9em;
  color: #bbb;
  margin-bottom: 20px;
}

.script-card-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.interaction-icons {
  display: flex;
  gap: 10px;
}

.interaction-icons span {
  display: flex;
  align-items: center;
  font-size: 0.9em;
}

.interaction-icons i {
  margin-right: 5px;
}

.play-button {
  background-color: #3b82f6;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 10px 15px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.play-button:hover {
  background-color: #2563eb;
}

.script-card {
  position: relative; /* 让内部的元素可以基于这个卡片进行绝对定位 */
  background-color: #2c2c2e;
  border-radius: 12px;
  overflow: hidden;
  width: 300px;
  margin: 10px;
  display: flex;
  flex-direction: column;
}

.delete-button {
  position: absolute;
  top: 10px;
  right: 10px;
  background-color: #ff4d4f;
  color: white;
  border: none;
  border-radius: 50%;
  width: 24px;
  height: 24px;
  cursor: pointer;
  font-size: 16px;
  line-height: 24px;
  text-align: center;
  display: none; /* 默认不显示 */
}

.script-card:hover .delete-button {
  display: block; /* 鼠标悬停时显示删除按钮 */
}

.play-button {
  background-color: #3b82f6;
  color: white;
  border: none;
  border-radius: 8px;
  padding: 10px 15px;
  cursor: pointer;
  transition: background-color 0.3s;
}

.play-button:hover {
  background-color: #2563eb;
}

.delete-modal-overlay{
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background: rgba(0, 0, 0, 0.6);
  z-index: 9999;
  display: flex;
  justify-content: center; /* 水平方向居中 */
  align-items: center; /* 垂直方向居中 */
}
.modal-content {
  background-color: #2b2b2b; /* 深灰色背景，类似于图片中样式 */
  padding: 20px;
  border-radius: 8px;
  width: 80%;
  max-width: 600px;
  box-shadow: 0 4px 8px rgba(0, 0, 0, 0.2);
  position: relative;
  color: #ffffff; /* 白色字体，与深色背景对比更强烈 */
}
.close {
  position: absolute;
  top: 10px;
  right: 15px;
  font-size: 24px;
  font-weight: bold;
  color: #ffffff;
  cursor: pointer;
}

.close:hover {
  color: #ff6b6b; /* 悬停时颜色变为浅红色，增强视觉反馈 */
}

.confirm-button {
  background-color: #e74c3c; /* 红色按钮，提示删除操作 */
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
  margin-top: 20px;
  transition: background-color 0.3s ease;
}

.confirm-button:hover {
  background-color: #c0392b;
}


.cancel-button {
  background-color: #555; /* 灰色按钮 */
  color: #fff;
  border: none;
  border-radius: 4px;
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
  margin-top: 20px;
  margin-left: 10px;
  transition: background-color 0.3s ease;
}

.cancel-button:hover {
  background-color: #444;
}
</style>
